package org.veridiandynamics.client.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.veridiandynamics.server.domain.DoctorsOrders;
import org.veridiandynamics.server.model.PatientModel;
import org.veridiandynamics.server.model.ReportModel;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;


/**
 * This class generates guis for doctors that enable them to view reports 
 * and add doctors orders to treatment records.
 * 
 * @author Lee
 */
public class DoctorPanel extends AbstractPanelLayout 
{
	private JButton viewNurseOperations;
	private DoctorPanel thisPanel;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JButton submit;
	private JTextField textField_4;
	
	private Map<String, Object> doctorsOrdersMap;
	private JTextField textField_5;
	private JTextField patientField;
	private JComboBox yearBox;
	private JComboBox monthBox;
	private JRadioButton rdbtnIncomeStatement;
	private JRadioButton rdbtnPatientHealthHistory;
	private String[] years = { "2011", "2012"};
	private String[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
	
	/**
	 * Constructor for the doctor panel.
	 */
	public DoctorPanel()
	{
		thisPanel = this;
		numCenterPanels = 1;
		initialize();
		panelLeft.setLayout(new BoxLayout(panelLeft, BoxLayout.Y_AXIS));
		
		
		JPanel subLeft = new JPanel();
		subLeft.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(135dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(129dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblViewReports = new JLabel("1.  View Reports");
		subLeft.add(lblViewReports, "4, 2");
		
		JLabel lblViewStatistics = new JLabel("2.  View Statistics");
		subLeft.add(lblViewStatistics, "8, 2");
		
		JLabel lblWhichReport = new JLabel("Which report?");
		subLeft.add(lblWhichReport, "4, 4");
		
		ButtonGroup group = new ButtonGroup();
		rdbtnPatientHealthHistory = new JRadioButton("Patient Health History");
		subLeft.add(rdbtnPatientHealthHistory, "4, 6");
		group.add(rdbtnPatientHealthHistory);
		
		panelLeft.add(subLeft);
		
		rdbtnIncomeStatement = new JRadioButton("Income Statement");
		subLeft.add(rdbtnIncomeStatement, "8, 6");
		group.add(rdbtnIncomeStatement);
		
		JPanel panel_1 = new JPanel();
		subLeft.add(panel_1, "4, 8, left, fill");
		
		JLabel lblWhichPatient = new JLabel("Which patient?");
		panel_1.add(lblWhichPatient);
		
		patientField = new JTextField();
		panel_1.add(patientField);
		patientField.setColumns(10);
		
		JPanel panel_2 = new JPanel();
		subLeft.add(panel_2, "8, 8, fill, fill");
		yearBox = new JComboBox(years);
	
		panel_2.add(yearBox);
		
		
		monthBox = new JComboBox(months);
		panel_2.add(monthBox);
		
		JPanel panel = new JPanel();
		subLeft.add(panel, "4, 14, fill, fill");
		
		JButton btnViewReport = new JButton("View Report");
		btnViewReport.addActionListener(new ActionListener()
		{
			/**
			 * Brings up the patient health history panel.
			 */
			public void actionPerformed(ActionEvent event)
			{
				if (rdbtnPatientHealthHistory.isSelected())
				{
				String patientName = patientField.getText();
//				Patient patient = new Patient();
//				HibernateDriver driver = new HibernateDriver();
//				long id = Queryprocessor.UsernametoID(patientName);
//				patient = (Patient) driver.RetrieveObject(id, patient);
//				patient.
				
				PatientModel model = new PatientModel();
				List<Integer> weightList = model.getWeightHistory(patientName);
				
				XYSeries series = new XYSeries("Patient Weight versus Time Graph");
				int i = 1;
				for (Integer e: weightList)
				{
					series.add(i, e);
					i++;
				}
				XYSeriesCollection dataset = new XYSeriesCollection();
				dataset.addSeries(series);
				JFreeChart chart = ChartFactory.createXYLineChart("Patient Time versus Weight Graph", "Appointment number", "Weight (pounds)", dataset, PlotOrientation.VERTICAL, true, true, false);
				ChartFrame frame = new ChartFrame("Patient History", chart); 
				frame.pack();
				frame.setVisible(true);
				}
				
				if (rdbtnIncomeStatement.isSelected())
				{
					String year = years[yearBox.getSelectedIndex()];
					int num = 0;
					if (year.equals("Feb")) num = 1;
					if (year.equals("Mar")) num = 2;
					if (year.equals("Apr")) num = 3;
					if (year.equals("May")) num = 4;
					if (year.equals("Jun")) num = 5;
					if (year.equals("Jul")) num = 6;
					if (year.equals("Aug")) num = 7;
					if (year.equals("Sep")) num = 8;
					if (year.equals("Oct")) num = 9;
					if (year.equals("Nov")) num = 10;
					if (year.equals("Dec")) num = 11;
					String month = months[monthBox.getSelectedIndex()];
					Date date = new Date(Integer.parseInt(year) - 1900, num, 1);
					ReportModel model = new ReportModel();
					Map<String, Double> info = model.incomeStatement(date);
					Double a = info.get("numberOfPatients");
					Double b = info.get("totalBilled");
					JOptionPane.showMessageDialog(null, "Patients seen " + a + ", total billed, " + b);
				}
			}
		});
		panel.add(btnViewReport);
		
		JLabel lblAddDoctors = new JLabel("2.  Add Doctors Orders");
		subLeft.add(lblAddDoctors, "4, 16");
		
		JLabel lblPrescriptions = new JLabel("Prescriptions");
		subLeft.add(lblPrescriptions, "4, 18");
		
		JLabel lblFollowUp = new JLabel("Follow Up");
		subLeft.add(lblFollowUp, "8, 18");
		
		textField = new JTextField();
		subLeft.add(textField, "4, 20, fill, default");
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		subLeft.add(textField_1, "8, 20, fill, default");
		textField_1.setColumns(10);
		
		JLabel lblLabWorkPerformed = new JLabel("Lab Work Performed");
		subLeft.add(lblLabWorkPerformed, "4, 22");
		
		JLabel lblOtherInstructions = new JLabel("Other Instructions");
		subLeft.add(lblOtherInstructions, "8, 22");
		
		textField_2 = new JTextField();
		subLeft.add(textField_2, "4, 24, fill, default");
		textField_2.setColumns(10);
		
		textField_3 = new JTextField();
		subLeft.add(textField_3, "8, 24, fill, default");
		textField_3.setColumns(10);
		
		JLabel lblTreatmentRecordId = new JLabel("Treatment Record ID:");
		subLeft.add(lblTreatmentRecordId, "4, 26");
		
		textField_4 = new JTextField();
		subLeft.add(textField_4, "4, 28, fill, default");
		textField_4.setColumns(10);
		
		JPanel subLeft2 = new JPanel();

		submit = new JButton("Submit");
		submit.addActionListener(new ActionListener() 
		{
			/**
			 * This method handles ading doctors orders to the treatment record.
			 */
			public void actionPerformed(ActionEvent event) 
			{
				String prescription = textField.getText();
				String followUP = textField_1.getText();
				String labWork = textField_2.getText();
				String other = textField_3.getText();
				Long id = Long.parseLong(textField_4.getText());
				
				doctorsOrdersMap = new HashMap<String, Object>();
				doctorsOrdersMap.put("requestType", "addOrders");
				DoctorsOrders orders = new DoctorsOrders(other, prescription, 
						labWork, followUP);
				doctorsOrdersMap.put("doctorsOrders", orders);
				doctorsOrdersMap.put("treatmentRecordID", id);
				
				JOptionPane.showMessageDialog(null, "Doctors Orders created" +
						" and added to the record");
			}
		});
		subLeft2.add(submit);
		panelLeft.add(subLeft2);
	}
}
